package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.CommonUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyCommunityUnitAddBo;
import com.ruoyi.system.bo.ZsyyCommunityUnitQueryBo;
import com.ruoyi.system.bo.ZsyyCommunityUnitEditBo;
import com.ruoyi.system.domain.ZsyyCommunityUnit;
import com.ruoyi.system.mapper.ZsyyCommunityUnitMapper;
import com.ruoyi.system.vo.ZsyyCommunityUnitVo;
import com.ruoyi.system.service.IZsyyCommunityUnitService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * 小区单元管理Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyCommunityUnitServiceImpl extends ServicePlusImpl<ZsyyCommunityUnitMapper, ZsyyCommunityUnit> implements IZsyyCommunityUnitService {
    @Autowired
    private ZsyyCommunityUnitMapper iZsyyCommunityUnitMapper;
    @Autowired
    private GetCommunityIdImpl getCommunityIdImpl;


    @Override
    public ZsyyCommunityUnitVo queryById(Long id){
        return getVoById(id, ZsyyCommunityUnitVo.class);
    }

    @Override
    public TableDataInfo<ZsyyCommunityUnitVo> queryPageList(ZsyyCommunityUnitQueryBo bo) {
		bo.setCommunityId(getCommunityIdImpl.getLoginUser().getCommunityId());
		Page<ZsyyCommunityUnitVo> result = iZsyyCommunityUnitMapper.selectListByQuery(PageUtils.buildPage(), bo);
		return PageUtils.buildDataInfo(result);
    }



    @Override
    public List<ZsyyCommunityUnitVo> queryList(ZsyyCommunityUnitQueryBo bo) {
		return iZsyyCommunityUnitMapper.queryListByExport(bo);
    }
    //根据小区id获取单元列表
    @Override
    public List<ZsyyCommunityUnit> getListByCommunity(ZsyyCommunityUnitQueryBo bo) {
		QueryWrapper<ZsyyCommunityUnit> wrapper = new QueryWrapper<>();
		wrapper.eq("community_id",bo.getCommunityId());
		return iZsyyCommunityUnitMapper.selectList(wrapper);
    }

    private LambdaQueryWrapper<ZsyyCommunityUnit> buildQueryWrapper(ZsyyCommunityUnitQueryBo bo) {
        Map<String, Object> params = bo.getParams();
		bo.setCommunityId(getCommunityIdImpl.getLoginUser().getCommunityId());
        LambdaQueryWrapper<ZsyyCommunityUnit> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getUnitName()), ZsyyCommunityUnit::getUnitName, bo.getUnitName());
        lqw.like(!CommonUtil.isEmpty(bo.getCommunityId()), ZsyyCommunityUnit::getCommunityId, bo.getCommunityId());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyCommunityUnitAddBo bo) {
        ZsyyCommunityUnit add = BeanUtil.toBean(bo, ZsyyCommunityUnit.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ZsyyCommunityUnitEditBo bo) {
        ZsyyCommunityUnit update = BeanUtil.toBean(bo, ZsyyCommunityUnit.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyCommunityUnit entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
